package com.g2room.io.grep;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.g2room.io.file.FileOp;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.security.MD5Encoder;
import sun.security.provider.MD5;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.net.URL;
import java.net.URLConnection;
import java.security.MessageDigest;
import java.util.List;
import java.util.Set;

public class GrepAmap extends GrepCommon {
    public static String DATA_URL = "https://restapi.amap.com/v3/place/text?key=57f4aa610048e45bf1ebd674bcfeb254&keywords=火锅&types=火锅店&city=北京&children=1&offset=50&extensions=all&page=";
    public static List list = Lists.newArrayList();

    public static void main(String[] args) {
        for (int i = 2; i < 19; i++) {
//            FileOp.appendContent("D:\\user\\temp\\1\\111.txt", i + "");
            getData(DATA_URL + i);
        }
    }

    public static void getData(String url) {
        InputStreamReader reader = null;
        BufferedReader in = null;
        try {
            URLConnection connection = new URL(url).openConnection();
            connection.setConnectTimeout(1000);
            reader = new InputStreamReader(connection.getInputStream(), "UTF-8");
            in = new BufferedReader(reader);
            String line = null; // 每行内容
            StringBuffer content = new StringBuffer();
            while ((line = in.readLine()) != null) {
                content.append(line);
            }
            if (StringUtils.isNotBlank(content)) {
                String jsonStr = content.toString().replaceAll("\\n", "");
                JSONObject jsonObject = JSONObject.parseObject(jsonStr);
                JSONArray jsonArray = jsonObject.getJSONArray("pois");
                String md5 = "";
                String text = "";
                for (Object obj : jsonArray) {
                    JSONObject jsObj = JSON.parseObject(obj.toString());
                    text = jsObj.getString("name")
                            + "@" + jsObj.getString("address")
                            + "@" + jsObj.getString("tel");
                    md5 = md5(text);
                    if (list.contains(md5)) {
                        System.out.println(content);
                        continue;
                    }
                    list.add(md5);
                    FileOp.appendContent("D:\\user\\temp\\1\\shoppers.txt", text);
                }
                System.out.println(jsonArray.size());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    System.out.println("关闭流出现异常!!!");
                }
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    System.out.println("关闭流出现异常!!!");
                }
            }
        }
    }

    /**
     * 对字符串md5加密
     *
     * @param str
     * @return
     */
    public static String md5(String str) {
        try {
            // 生成一个MD5加密计算摘要
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 计算md5函数
            md.update(str.getBytes());
            // digest()最后确定返回md5 hash值，返回值为8为字符串。因为md5 hash值是16位的hex值，实际上就是8位的字符
            // BigInteger函数则将8位的字符串转换成16位hex值，用字符串来表示；得到字符串形式的hash值
            String md5Str = new BigInteger(1, md.digest()).toString(16);
            int len = md5Str.length();
            if (len < 32) {
                for (int i = 0; i < 32 - len; i++) {
                    md5Str = "0" + md5Str;
                }
            }
            return md5Str;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}
